﻿using System.Data;
using System.Data.SQLite;
using System.Text;
using Stock.DBUtility;
using Stock.Model;

namespace Stock.Dal
{
    /// <summary>
    /// 数据访问类:StockInfoDal
    /// </summary>
    public partial class StockInfoDal
    {
        public StockInfoDal()
        { }

        #region Method

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(StockInfoEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into StockInfo(");
            strSql.Append("S_Code,S_Name,S_PYM)");
            strSql.Append(" values (");
            strSql.Append("@S_Code,@S_Name,@S_PYM)");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@S_Code", DbType.String),
                    new SQLiteParameter("@S_Name", DbType.String),
                    new SQLiteParameter("@S_PYM", DbType.String)};
            parameters[0].Value = model.S_Code;
            parameters[1].Value = model.S_Name;
            parameters[2].Value = model.S_PYM;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(StockInfoEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update StockInfo set ");
            strSql.Append("S_Name=@S_Name,");
            strSql.Append("S_PYM=@S_PYM");
            strSql.Append(" where S_Code=@S_Code ");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@S_Name", DbType.String),
                    new SQLiteParameter("@S_PYM", DbType.String),
                    new SQLiteParameter("@S_Code", DbType.String)};
            parameters[0].Value = model.S_Name;
            parameters[1].Value = model.S_PYM;
            parameters[2].Value = model.S_Code;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string S_Code)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from StockInfo ");
            strSql.Append(" where S_Code=@S_Code ");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@S_Code", DbType.String)};
            parameters[0].Value = S_Code;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string S_Codelist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from StockInfo ");
            strSql.Append(" where S_Code in (" + S_Codelist + ")  ");
            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public StockInfoEntity GetModel(string S_Code)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select S_Code,S_Name,S_PYM from StockInfo ");
            strSql.Append(" where S_Code=@S_Code ");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@S_Code", DbType.String)};
            parameters[0].Value = S_Code;

            StockInfoEntity model = new StockInfoEntity();
            DataSet ds = DbHelperSQLite.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["S_Code"] != null && ds.Tables[0].Rows[0]["S_Code"].ToString() != "")
                {
                    model.S_Code = ds.Tables[0].Rows[0]["S_Code"].ToString();
                }
                if (ds.Tables[0].Rows[0]["S_Name"] != null && ds.Tables[0].Rows[0]["S_Name"].ToString() != "")
                {
                    model.S_Name = ds.Tables[0].Rows[0]["S_Name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["S_PYM"] != null && ds.Tables[0].Rows[0]["S_PYM"].ToString() != "")
                {
                    model.S_PYM = ds.Tables[0].Rows[0]["S_PYM"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select S_Code,S_Name,S_PYM ");
            strSql.Append(" FROM StockInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQLite.Query(strSql.ToString());
        }

        #endregion Method

        internal void DeleteAll()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from StockInfo ");
            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString());
        }

        internal StockInfoEntity GetModelByWhere(string wherestr)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select S_Code,S_Name,S_PYM from StockInfo ");
            strSql.Append(" where ").Append(wherestr);

            StockInfoEntity model = new StockInfoEntity();
            DataSet ds = DbHelperSQLite.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["S_Code"] != null && ds.Tables[0].Rows[0]["S_Code"].ToString() != "")
                {
                    model.S_Code = ds.Tables[0].Rows[0]["S_Code"].ToString();
                }
                if (ds.Tables[0].Rows[0]["S_Name"] != null && ds.Tables[0].Rows[0]["S_Name"].ToString() != "")
                {
                    model.S_Name = ds.Tables[0].Rows[0]["S_Name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["S_PYM"] != null && ds.Tables[0].Rows[0]["S_PYM"].ToString() != "")
                {
                    model.S_PYM = ds.Tables[0].Rows[0]["S_PYM"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }
    }
}